/**
 * WebSocket
 *
 * @author mhh
 */

function createWebSocket(wsDomain, wsPort) {
    //console.log(mhhEncode(nickName));
    var wsUri = "ws://" + wsDomain + ":" + wsPort + "/coquettish/websocket/connect";// + mhhEncode(nickName);
    websocket = new WebSocket(wsUri);

    websocket.onopen = function(evt) {
        console.log("WebSocket Server Connected");
    };

    websocket.onmessage = function(evt) {
        console.log("WebSocket Server Send Message: " + evt.data);
        var charMessage = $.parseJSON(evt.data);
        if (charMessage != null) {
            var head = charMessage.head;
            if (head.msg) {
                alert(head.msg);
                unregist();
                return;
            }
            var body = charMessage.body;
            var contents = body.contents;
            for ( var index in contents) {
                var content = contents[index];
                if (content != null) {
                    var messageType = content.messageType;
                    if (messageType == 1) { //上线
                        $("#tblOnline").find("tbody").empty();
                        $(content.chater).each(function(index, val) {
                            $("#tblOnline").find("tbody").append("<tr><td align='left' style='border: 1px solid black'>" + val + "</td></tr>");
                        });
                        $("#lblOnlineCount").text(content.chater.length);
                        $("#tblChart").find("tbody").append("<tr><td align='center' style='color: red'>" + content.message + " 上线了</td></tr>");
                    } else if (messageType == 2) { //下线
                        $("#tblOnline").find("tbody").empty();
                        $(content.chater).each(function(index, val) {
                            $("#tblOnline").find("tbody").append("<tr><td align='left' style='border: 1px solid black'>" + val + "</td></tr>");
                        });
                        $("#lblOnlineCount").text(content.chater.length);
                        $("#tblChart").find("tbody").append("<tr><td align='center' style='color: red'>" + content.message + " 下线了</td></tr>");
                    } else if (messageType == 3) { //消息
                        if (content.isSender == 0) {
                            $("#tblChart").find("tbody").append("<tr><td align='left'>" + content.sender + ": " + content.message + "</td></tr>");
                        } else if (content.isSender == 1) {
                            $("#tblChart").find("tbody").append("<tr><td align='right'>" + content.sender + ": " + content.message + "</td></tr>");
                        }
                    } else if (messageType == 4) { //历史消息
                        $(content.messageHis).each(function(index, val) {
                            $("#tblChart").find("tbody").append("<tr><td align='center' style='color: gray'>" + val.sendTime + "</td></tr>");
                            $("#tblChart").find("tbody").append("<tr><td align='left' style='color: gray'>" + val.message + "</td></tr>");
                        });
                        if (content.messageHis.length > 0) {
                            $("#tblChart").find("tbody").append("<tr><td align='center' style='color: gray'>------以上为历史消息------</td></tr>");
                        }
                    }
                    $("#divChart").scrollTop($("#divChart")[0].scrollHeight);
                }
            }
        }
    };

    websocket.onerror = function(evt) {
        console.log("WebSocket ERROR: " + evt.data);
    };

    websocket.onclose = function(event) {
        console.log("WebSocket CLOSE");
    };
}